package main

import (
	"bufio"
	"fmt"
	"img_crawler/util"
	"os"
	"strconv"
	"time"
)

var (
	startPage, endPage int
)

func main() {
	fmt.Println("请输入开始页面")
	inputReader := bufio.NewReader(os.Stdin)
	data, _, _ := inputReader.ReadLine()
	command, err := strconv.Atoi(string(data))
	if err != nil {
		fmt.Println("格式不对，请输入数字")
		return
	}
	if command < 100 {
		fmt.Println("请输入大于100")
		return
	}
	startPage = command
	fmt.Println("请输入结束页面")
	data, _, _ = inputReader.ReadLine()
	command, err = strconv.Atoi(string(data))
	if err != nil {
		fmt.Println("格式不对，请输入数字")
		return
	}
	if command < 101 {
		fmt.Println("请输入大于101")
		return
	}
	endPage = command

	startTime := time.Now()

	urls := util.GetUrls(startPage, endPage)

	path := util.GetUserDesktopPath() + "\\imgs\\"

	if !util.Exists(path) {
		_ = util.Mkdir(path)
	}

	for _, url := range urls {
		util.ParseHtml(url, path)
	}

	endTime := time.Since(startTime)

	fmt.Println("程序耗时:", endTime)
	fmt.Println("图片存储位置:", path)
	fmt.Println("共计爬取页面:", endPage-startPage)
	select {}
}
